package kz.kstu.scantest.business.entity.test;

import kz.kstu.scantest.business.entity.Identifiable;
import kz.kstu.scantest.business.entity.data.SubjectEnum;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Filipp_Stankevich
 *         created: 12.09.11 19:49
 */
@Entity
@Table(name = "SUBJECT_QUESTION")
public class SubjectQuestion implements Identifiable {

    private Long id;
    private SubjectEnum subject;
    private Test test;
    private List<Question> questions;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    @Enumerated
    @Column(name = "SUBJECT_ID")
    public SubjectEnum getSubject() {
        return subject;
    }

    public void setSubject(SubjectEnum subject) {
        this.subject = subject;
    }

    @ManyToOne
    @JoinColumn(name = "TEST_ID", referencedColumnName = "ID")
    public Test getTest() {
        return test;
    }

    public void setTest(Test test) {
        this.test = test;
    }

    @OrderBy(value = "questionNumber")
    @OneToMany(mappedBy = "subjectQuestion", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public List<Question> getQuestions() {
        return questions;
    }

    public void setQuestions(List<Question> questions) {
        this.questions = questions;
    }

    public void addQuestion(Question question) {
        if (questions == null) {
            questions = new ArrayList<Question>();
        }
        question.setSubjectQuestion(this);
        questions.add(question);
    }
}
